package com.jochen.core.system

import com.artemis.Aspect
import com.artemis.annotations.Wire
import com.artemis.systems.IteratingSystem
import com.jochen.core.components.*
import com.jochen.core.world.GdxGame
import com.jochen.core.assets.*
import com.jochen.core.audio.AudioManager

class BuildingSystem : IteratingSystem(Aspect.all(Building::class.java)) {
    
    @Wire
    private lateinit var game: GdxGame
    
    lateinit var audioManager: AudioManager
    lateinit var assetService: AssetService
    
    override fun initialize(){
        audioManager = game.audioManager
        assetService = game.assetService
    }
    
    override fun process(entityId: Int) {
        val building = Mapper.buildings.get(entityId)
        if(building.SwitchRegion) updateRegion(entityId,building)
        
    }
    
    private fun updateRegion(entityId: Int, building: Building) {
        val sprite = Mapper.sprites.get(entityId)
        sprite?.let { s ->
            val builData = building.buildingData
            if (building.isConstructionFinished) {
                val regionPath = builData.levelRegion.get(building.level - 1)
                s.region = assetService.get(AtlasAsset.OBJECT).findRegion(regionPath)
            } else {
                s.region = assetService.get(AtlasAsset.OBJECT).findRegion(builData.unRegion)
            }
        }
    }
    
}